//封装网络请求request
function createRequest(options = {}) {
  const defaultOptions = {
    baseURL: '', // 基础URL
    timeout: 5000, // 超时时间
    header: {}, // 请求头
  };

  const mergedOptions = { ...defaultOptions, ...options };

  return function request(url, method, data, header = {}) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: mergedOptions.baseURL + url,
        method,
        data,
        header: { ...mergedOptions.header, ...header },
        timeout: mergedOptions.timeout,
        success: (res) => {
          resolve(res.data);
        },
        fail: (error) => {
          reject(error);
        },
      });
    });
  };
}

export default createRequest;